package com.turboconnard.display;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import com.turboconnard.tools.Textures;

public class Cube extends Sprite {
	/** The buffer holding the vertices */
	private FloatBuffer vertexBuffer;
	/** The buffer holding the texture coordinates */
	private FloatBuffer textureBuffer;
	/** The buffer holding the indices */
	private ByteBuffer indexBuffer;
	
	/** Our texture pointer */
	//private int[] textures = new int[1];
	
	public int texture;
	/** The initial texture coordinates (u, v) */	
    private float textureMap[] = {    		
			    		//Mapping coordinates for the vertice
    					0.0f, 1.0f,
    					1.0f, 1.0f,
    					0.0f, 0.0f,
    					1.0f, 0.0f,
  
			    		0.0f, 0.0f,
			    		0.0f, 1.0f,
			    		1.0f, 0.0f,
			    		1.0f, 1.0f,
			    		
			    		0.0f, 0.0f,
			    		0.0f, 1.0f,
			    		1.0f, 0.0f,
			    		1.0f, 1.0f,
			    		
			    		0.0f, 0.0f,
			    		0.0f, 1.0f,
			    		1.0f, 0.0f,
			    		1.0f, 1.0f,
			    		
			    		0.0f, 0.0f,
			    		0.0f, 1.0f,
			    		1.0f, 0.0f,
			    		1.0f, 1.0f,
			    		
			    		0.0f, 0.0f,
			    		0.0f, 1.0f,
			    		1.0f, 0.0f,
			    		1.0f, 1.0f,

			    							};
        
    /** The initial indices definition */	
    private byte indices[] = {
    					//Faces definition
			    		0,1,3, 0,3,2, 			//Face front
			    		4,5,7, 4,7,6, 			//Face right
			    		8,9,11, 8,11,10, 		//... 
			    		12,13,15, 12,15,14, 	
			    		16,17,19, 16,19,18, 	
			    		20,21,23, 20,23,22, 	
    										};
	public float depth;
	public float height;
	public float width;
	protected float[] vertices;
	/**
	 * The Cube constructor.
	 * 
	 * Initiate the buffers.
	 */
	public Cube() {
		
	}
	public Cube(float pSize) {
		float unit = pSize*stage.correction/2;
		width = height = depth = pSize;
		float[] verts = {
				//Vertices according to faces
	    		-unit, -unit, unit, //Vertex 0
	    		unit, -unit, unit,  //v1
	    		-unit, unit, unit,  //v2
	    		unit, unit, unit,   //v3
	    		
	    		unit, -unit, unit,	//...
	    		unit, -unit, -unit,    		
	    		unit, unit, unit,
	    		unit, unit, -unit,
	    		
	    		unit, -unit, -unit,
	    		-unit, -unit, -unit,    		
	    		unit, unit, -unit,
	    		-unit, unit, -unit,
	    		
	    		-unit, -unit, -unit,
	    		-unit, -unit, unit,    		
	    		-unit, unit, -unit,
	    		-unit, unit, unit,
	    		
	    		-unit, -unit, -unit,
	    		unit, -unit, -unit,    		
	    		-unit, -unit, unit,
	    		unit, -unit, unit,
	    		
	    		-unit, unit, unit,
	    		unit, unit, unit,    		
	    		-unit, unit, -unit,
	    		unit, unit, -unit,
		};
		vertices = verts;
		defineBuf();
	}
	protected void defineBuf() {
		
		ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		vertexBuffer = byteBuf.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);

		byteBuf = ByteBuffer.allocateDirect(textureMap.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		textureBuffer = byteBuf.asFloatBuffer();
		textureBuffer.put(textureMap);
		textureBuffer.position(0);

		indexBuffer = ByteBuffer.allocateDirect(indices.length);
		indexBuffer.put(indices);
		indexBuffer.position(0);
	}
	protected void _draw(GL10 gl) {
		
		//Bind our only previously generated texture in this case
		
		/*
			gl.glEnable(GL10.GL_BLEND);
			gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
			gl.glEnable(GL10.GL_TEXTURE_2D);
			gl.glBindTexture(GL10.GL_TEXTURE_2D, Textures.getInstance().getTexture(texture));
			*/
			int mode = GL10.GL_TRIANGLES;
			
			//Point to our buffers
		//	gl.glColorPointer(4, GL10.GL_FIXED, 0, null);
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	
			//Set the face rotation
			//gl.glFrontFace(GL10.GL_CCW);
			//Enable the vertex and texture state
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
			//Draw the vertices as triangles, based on the Index Buffer information
			gl.glDrawElements(mode, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
		
			//Disable the client state before leaving
			gl.glDisable(GL10.GL_TEXTURE_2D);
			gl.glDisable(GL10.GL_BLEND);
			gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
	}
}
